function y=genf(p, t, a) //p:state reached by action a at time t
    y=zeros(ws,1);
    //cnt=2;
    //y(1)=k;
    //y(1)=0;
    if (p>=0 & p<gsize & t>0 & t<=3*gsize) then //loadp should be executed first
        if (strcmp(part(grid(t+1),p+1),"X")==0) then
            //y(1)=-1000;
            y(1)=-1;
        elseif (strcmp(part(grid(t+1),p+1),"*")==0) then
            //y(3)=1000;
            y(3)=1;
        //elseif (strcmp(part(grid(t+2),p+1),"X")==0) then
        //    y(1)=-5;
        //else
        //    y(1)=1;
        end
        if (strcmp(part(grid(t+1),p+1-a),"X")==0 & strcmp(part(grid(t),p+1),"X")==0) then
            //y(1)=y(1)-1000;
            y(1)=-1;
        end
    end
    //cnt=1;
    y(2)=-abs(a);
    //y(cnt+4)=0;
    //if p==gsize+1 then
    //    y(4)=1000;
    //end
    //y(4)=a;
    //y(5)=-1;
    //y(cnt+3)=1;
endfunction
function rw=evalr(pls)
    //pls nx2 matrix, position and action
    //return a row
    rw=zeros(ws,1);
    s=0;
    for t=1:size(pls,1)
        s=s+pls(t);
        //rw=rw+gm*genf(s,t,pls(t));
        rw=rw+genf(s,t,pls(t));
    end
    //rw=rw/size(pls,1);
    rw=rw';
endfunction
function rw=evalr2(pls)
    rw=zeros(ws,1);
    for i=1:100
        s=0;
        for t=1:3*gsize-1
            a=pls(s+1,t)+2;
            pr=rand();
            if pr<pact(s+1,a,1)
                a=-1;
            elseif pr<sum(pact(s+1,a,1:2))
                a=0;
            else 
                a=1;
            end
            s=s+a;
        //rw=rw+gm*genf(s,t,pls(t));
            rw=rw+genf(s,t,a);
        end
    end
    //rw=rw/size(pls,1);
    rw=rw'/100;
endfunction

